import sys

import cv2     # opencv-python==4.2.0.34
import os

mainFolder = 'D:\\data\\test2'
# 遍历images文件夹下的所有文件夹
myFolders = os.listdir(mainFolder)
# 输出所有子文件夹的名称
print(myFolders)

# 准备遍历每个子文件夹下的图片
for folder in myFolders:
    if(os.path.isfile(folder)):
        continue
    path = mainFolder + '\\' + folder
    images = []
    # 遍历子文件夹下的图片名称
    myList = os.listdir(path)
    # 输出子文件内有多少张图片
    print(f'total number of images detected {len(myList)}')
    # 读取每张图片
    for imgName in myList:
        curImg = cv2.imread(f'{path}/{imgName}')
        #curImg = cv2.resize(curImg,(0,0),None,0.2,0.2)  # 重新设置图片大小
        images.append(curImg)  # 将每个子文件夹下的图片存进images数组中
        imgExt = os.path.splitext(imgName)[-1]
if(len(images)==0):
    print('images is empty')
    sys.exit()
stitcher = cv2.Stitcher.create()  # 创建一个stitcher实例     
(status, result) = stitcher.stitch(images)   
if (status == 0):       # 状态码为0的时候，进行处理
	print('Panorma Generated')
	print(imgExt)
	cv2.imwrite(mainFolder + "/out"+imgExt,result)
	cv2.imshow(folder, result)#cv2.WND_PROP_FULLSCREEN
else:
	print('Panorma Generated Unsuccessful')
cv2.waitKey(0)